package com.cms.admin.action.voice;

import com.aliyun.oss.model.PutObjectResult;
import com.cms.admin.action.BaseAction;
import com.cms.admin.model.PageBean;
import com.cms.admin.model.VoiceOnDemond;
import com.cms.admin.model.VoiceOnDemondExt;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.voice.VoiceService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY on 2017/7/14.
 */
public class VoiceSubjectAction extends BaseAction implements ModelDriven<VoiceOnDemondExt> {


    private VoiceService voiceService;
    private TemplateService templateService;
    private VoiceOnDemondExt search = new VoiceOnDemondExt();
    private PageBean page;
    private VoiceOnDemond voiceOnDemond;

    public String ajaxQueryVoiceSubjectList() {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        page = new PageBean(search.getCurrentPage(), search.getPageSize());

        List<VoiceOnDemond> voiceOnDemonds = voiceService.queryVoiceOnDemondList(page, search);


        String res = null;
        try {
            res = getQueryResult(voiceOnDemonds, page);
            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryUnGoingOrder", e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    public String ajaxUpdataVoiceSubject() {

        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        //上传图片到OSS
        if (null != search.getImgUrlImage()) {
            String imgUrl = null;
            try {
                imgUrl = uploadVoiceSubjectImgUrl(search.getImgUrlImage(), search.getImgUrlImageFileName());
            } catch (Exception e) {
                LOG.error("uploadStuffIcon", e);
            }
            imgUrl = null == imgUrl ? "" : imgUrl;
            search.setImgUrl(imgUrl);
        }

        voiceService.uploadVoiceSubject(search);
        JSONObject o = new JSONObject();
        o.put("errorCode", 0);
        o.put("errorMsg", "Success");
        return ajaxJsonSuccessMessage(o);
    }

    public String ajaxAddVoiceSubject() {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        //上传图片到OSS
        if (null != search.getImgUrlImage()) {
            String imgUrl = null;
            try {
                imgUrl = uploadVoiceSubjectImgUrl(search.getImgUrlImage(), search.getImgUrlImageFileName());
            } catch (Exception e) {
                LOG.error("uploadStuffIcon", e);
            }
            imgUrl = null == imgUrl ? "" : imgUrl;
            search.setImgUrl(imgUrl);

        }
        search.setIsUsedFlag((short) 1);
        voiceService.addVoiceSubject(search);
        LOG.info("新增了视频专题ID:" + search.getRecid());
        if (search.getRecid() > 0) {
            JSONObject o = new JSONObject();
            o.put("errorCode", 0);
            o.put("errorMsg", "Success");
            o.put("recid", search.getRecid());
            return ajaxJsonSuccessMessage(o);
        } else {
            JSONObject o = new JSONObject();
            o.put("errorCode", -1);
            o.put("errorMsg", "新增专题失败");
            return ajaxJsonSuccessMessage(o);
        }
    }

    public String ajaxDeleteVoiceSubject() {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        VoiceOnDemond voiceOnDemond = voiceService.queryVoiceOnDemondDetail(search.getRecid());
        if (null == voiceOnDemond) {
            JSONObject o = new JSONObject();
            o.put("errorCode", -1);
            o.put("errorMsg", "该音频专题信息不存在");
            return ajaxJson(o);
        } else {
            voiceOnDemond.setIsUsedFlag((short) 2);
            voiceService.uploadVoiceSubject(voiceOnDemond);
            JSONObject o = new JSONObject();
            o.put("errorCode", 0);
            o.put("errorMsg", "删除成功");
            return ajaxJson(o);
        }
    }

    private String getQueryResult(List<VoiceOnDemond> voiceOnDemonds, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("voiceOnDemondList", voiceOnDemonds);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_VOICEONDEMOND_QUERY_RESULT, map);
        return result;
    }

    public String ajaxQueryVoiceSubjectDetail() {
        voiceOnDemond = voiceService.queryVoiceOnDemondDetail(search.getRecid());
        if (!StringUtils.isEmpty(voiceOnDemond.getImgUrl())) {
            if (voiceOnDemond.getImgUrl().startsWith("http")) {
                voiceOnDemond.setFullImgUrl(voiceOnDemond.getImgUrl());
            } else {
                voiceOnDemond.setFullImgUrl("http://res.shouerhealth.cn/" + voiceOnDemond.getImgUrl());
            }
        }

        return "success";
    }

    public String addVoiceSubject() {
        return SUCCESS;
    }

    private String uploadVoiceSubjectImgUrl(File img, String fileName) {
        //后缀
        try {
            String tailSuffix = getExtension(fileName);
            String DIR = "image/voice/" + System.currentTimeMillis() + tailSuffix;
            PutObjectResult result = uploadByOss(img, DIR);
            if (null != result) {
                return DIR;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public String getVoiceSubjectList() {
        return SUCCESS;
    }

    public VoiceService getVoiceService() {
        return voiceService;
    }

    public void setVoiceService(VoiceService voiceService) {
        this.voiceService = voiceService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public VoiceOnDemondExt getSearch() {
        return search;
    }

    public void setSearch(VoiceOnDemondExt search) {
        this.search = search;
    }

    public VoiceOnDemond getVoiceOnDemond() {
        return voiceOnDemond;
    }

    public void setVoiceOnDemond(VoiceOnDemond voiceOnDemond) {
        this.voiceOnDemond = voiceOnDemond;
    }

    @Override
    public VoiceOnDemondExt getModel() {
        return search;
    }
}
